# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::canvas::WX;
use strict;
use canvas::Generic;
@dcExplorer::canvas::WX::ISA = qw(dcExplorer::canvas::Generic);

use Wx;
use Wx qw(wxNullPen wxNullBrush);
use Wx qw(wxTRANSPARENT_BRUSH wxBLACK_BRUSH);
use Wx qw(wxTRANSPARENT wxSOLID wxSTIPPLE wxBDIAGONAL_HATCH wxCROSSDIAG_HATCH wxFDIAGONAL_HATCH wxCROSS_HATCH wxHORIZONTAL_HATCH wxVERTICAL_HATCH);
use canvas::Color;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self  = $class->SUPER::new(@_);
	$self->{Image}->SetBackground(wxBLACK_BRUSH);
	$self->{Image}->Clear;
	bless ($self, $class);
	return $self;
}

sub color
{
	my $self = shift;
	my $color = shift;
	my $wxColour = Wx::Colour->new($color->{r}, $color->{g}, $color->{b}, $color->{a});
	return $wxColour;
}

sub _fgcolor
{
	my $self = shift;
	my $color = shift;
	my $pen = defined($color) ? Wx::Pen->new($self->color($color), 1, 1) : wxNullPen;
	$self->{Image}->SetPen($pen);
}

sub _bgcolor
{
	my $self = shift;
	my $color = shift;
	my $brush = defined($color) ? Wx::Brush->new($self->color($color), wxSOLID) : wxTRANSPARENT_BRUSH; #wxBLACK_BRUSH
	$self->{Image}->SetBrush($brush);
	$self->{Image}->SetBackground($brush);
}

sub Circle
{
	my $self = shift;
	my ($x, $y, $r, $fg, $bg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	$self->_bgcolor($bg);
	my $size = int(($self->{bsize} - 1) * $r / 200) + 1;
	$self->{Image}->DrawEllipse($x - $size, $y - $size, 2 * $size, 2 * $size);
}

sub Line
{
	my $self = shift;
	my ($x1, $y1, $x2, $y2, $fg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
#	$self->_fgcolor($fg);
	$self->{Image}->SetPen(defined($fg) ? Wx::Pen->new($self->color($fg), $penSize, 1) : wxNullPen);
	$self->{Image}->DrawLine($x1, $y1, $x2, $y2);
}

sub Rectangle
{
	my $self = shift;
	my ($x1, $y1, $x2, $y2, $fg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	$self->_bgcolor(undef);
	$self->{Image}->SetPen(defined($fg) ? Wx::Pen->new($self->color($fg), $penSize, 1) : wxNullPen);
	$self->{Image}->DrawRectangle($x1, $y1, $x2-$x1+1, $y2-$y1+1);
}

sub Poly
{
	my $self = shift;
	my ($points, $fg, $bg, $penSize) = @_;
	$penSize = 1 unless defined($penSize);
	$self->_fgcolor($fg);
	#$self->_bgcolor($bg);
	$self->_bgcolor(undef);
	$self->{Image}->DrawPolygon($points, 0, 0);
}

sub Text
{
	my $self = shift;
	my ($x, $y, $jx, $jy, $text, $color) = @_;
	$self->{Image}->SetTextForeground($self->color($color));
	my ($w, $h, $d, $e) = $self->{Image}->GetTextExtent($text);
	$self->{Image}->DrawText($text, $x - $w * ($jx + 1) / 2, $y - $h * ($jy + 1) / 2);
}

1;
